package com.eims.sls_app.ui.adapter;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.jar.Attributes.Name;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.android.volley.toolbox.RequestParams;
import com.eims.sls_app.dataaccess.Schemes;
import com.eims.sls_app.dataaccess.CircleBean.CommentBean;
import com.eims.sls_app.dataaccess.CircleBean.SchemeinfoBean;
import com.eims.sls_app.dataaccess.CircleBean.TopicBean;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.ui.CirclePersonInfo;
import com.eims.sls_app.ui.CommentActivity;
import com.eims.sls_app.ui.DocumentaryActivity;
import com.eims.sls_app.ui.LoginActivity;
import com.eims.sls_app.ui.RECommentActivity;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.AppTools;
import com.eims.sls_app.utils.ImageManager;
import com.eims.sls_app.utils.SnsPopupWindow;

import com.eims.sls_app.view.MyListView2;
import com.eims.sls_app.view.MyToast;
import com.eims.sls_app.view.XCRoundImageView;

import bls.juejin.cn.R;
import progressBar.onSnsBackListen;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager.OnActivityResultListener;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;
import android.widget.Toast;

public class CircleAdapter extends BaseAdapter {
	private Context context;
	private static final String TAG = "CircleAdapter";
	private List<TopicBean> topicBeans;
	private List<SchemeinfoBean> schemeinfoBeans;
	private List<List<CommentBean>> commentBeanLists;
	private String opt, auth, time, info, imei, crc; // 格式化后的参数
	private MyHandler mHandler;
	private MyAsynTask mMyAsynTask;
	private GuanzhuAsynTask guanzhuAsynTask;
	private guanHuMyHandler gzmHandler;
	private int selectLoctonParse;

	public CircleAdapter(Context context, List<TopicBean> topicBeans, List<SchemeinfoBean> schemeinfoBeans,
			List<List<CommentBean>> commentBeanLists) {
		super();
		this.context = context;
		this.topicBeans = topicBeans;
		this.schemeinfoBeans = schemeinfoBeans;
		this.commentBeanLists = commentBeanLists;

		mHandler = new MyHandler();
		gzmHandler = new guanHuMyHandler();
		// setDate(topicBeans, schemeinfoBeans, commentBeanLists);
	}

	public void addDate(List<TopicBean> topicBeans, List<SchemeinfoBean> schemeinfoBeans,
			List<List<CommentBean>> commentBeanLists) {
		this.topicBeans.addAll(topicBeans);
		this.schemeinfoBeans.addAll(schemeinfoBeans);
		this.commentBeanLists.addAll(commentBeanLists);

	}

	public void setComm(int position, CommentBean commentBean) {
		commentBeanLists.get(position).add(commentBean);
		notifyDataSetChanged();

	}

	public void setDate(List<TopicBean> topicBean, List<SchemeinfoBean> schemeinfoBean,
			List<List<CommentBean>> commentBeanList) {
		if (null == topicBeans)
			topicBeans = new ArrayList<TopicBean>();
		if (null == schemeinfoBeans)
			schemeinfoBeans = new ArrayList<SchemeinfoBean>();
		if (null == commentBeanLists) {
			commentBeanLists = new ArrayList<List<CommentBean>>();
		}
		topicBeans.clear();
		schemeinfoBeans.clear();
		commentBeanLists.clear();
		for (TopicBean str : topicBean) {
			topicBeans.add(str);
		}

		for (SchemeinfoBean schemeinfo : schemeinfoBean) {
			schemeinfoBeans.add(schemeinfo);
		}
		for (List<CommentBean> li : commentBeanList) {
			List<CommentBean> l = new ArrayList<CommentBean>();
			for (CommentBean str : l) {
				l.add(str);
			}
			commentBeanLists.add(l);
		}
	}

	public List<TopicBean> getTopicBeans() {
		return topicBeans;
	}

	@Override
	public int getCount() {
		if (topicBeans == null) {
			return 0;
		}
		return topicBeans.size();
	}

	@Override
	public Object getItem(int position) {

		return position;
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	/**
	 * @param str
	 * @return
	 */
	private SpannableStringBuilder addClickablePart(String str) {
		// 第一个赞图标
		SpannableString spanStr = setImageSpan();

		SpannableStringBuilder ssb = new SpannableStringBuilder(spanStr);
		ssb.append(str);
		String[] likeUsers = str.split(",");

		if (likeUsers.length > 0) {
			// 最后一个
			for (int i = 0; i < likeUsers.length; i++) {
				final String name = likeUsers[i];
				final int start = str.indexOf(name) + spanStr.length();
				ssb.setSpan(new ClickableSpan() {

					@Override
					public void onClick(View widget) {
//						Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
					}

					@Override
					public void updateDrawState(TextPaint ds) {
						super.updateDrawState(ds);
						ds.setColor(context.getResources().getColor(R.color.praise_item)); // 设置文本颜色
						// 去掉下划线
						ds.setUnderlineText(false);
					}

				}, start, start + name.length(), 0);
			}
		}
		return ssb;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		ViewHolder holder;
		// 判断View是否为空
		if (view == null) {
			holder = new ViewHolder();
			final SnsPopupWindow snsPopupWindow = holder.snsPopupWindow;
			// LayoutInflater inflater = LayoutInflater.from(context);
			// 得到布局文件
			view = LayoutInflater.from(context).inflate(R.layout.adapter_circle_item, null);
			holder.iv_Head = (XCRoundImageView) view.findViewById(R.id.iv_item_head);
			holder.tv_name = (TextView) view.findViewById(R.id.nameTv);
			holder.tv_detail = (TextView) view.findViewById(R.id.tv_item_detail);
			holder.tv_stoptime = (TextView) view.findViewById(R.id.tv_item_stoptime);
			holder.tv_Total_amount = (TextView) view.findViewById(R.id.tv_Total_amount);
			holder.tv_Projected_bonus = (TextView) view.findViewById(R.id.tv_Projected_bonus);
			holder.tv_price = (TextView) view.findViewById(R.id.tv_Price);
			holder.tv_item_documentary = (TextView) view.findViewById(R.id.tv_item_documentary);
			holder.tv_circle_time = (TextView) view.findViewById(R.id.tv_circle_time);

			holder.snsBtn = (ImageView) view.findViewById(R.id.snsBtn);
			holder.snsBtnSay = (ImageView) view.findViewById(R.id.snsBtn_say);
			holder.tvpraiseTextView = (TextView) view.findViewById(R.id.praiseTextView);
			holder.lin_dig = view.findViewById(R.id.lin_dig);
			holder.commentList = (MyListView2) view.findViewById(R.id.commentList);
			holder.digCommentBody = (LinearLayout) view.findViewById(R.id.digCommentBody);
			holder.ll_even_red = (LinearLayout) view.findViewById(R.id.ll_even_red);
			holder.tv_number = (TextView) view.findViewById(R.id.tv_even_red_number);
			holder.tvGuanzhu = (TextView) view.findViewById(R.id.ll_tv_ganzhu);
			holder.ivTypeId = (ImageView) view.findViewById(R.id.iv_type_id);
			
			holder.ivHongbaoShow=(ImageView) view.findViewById(R.id.iv_hongbao_show);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		if (topicBeans.get(position).getPlayTypeID().startsWith("72")) {
			holder.ivTypeId.setBackgroundResource(R.drawable.bet_loty_logo_jczq_main);
			holder.tv_item_documentary.setText("跟单");
			holder.tv_item_documentary.setEnabled(true);
		} else if (topicBeans.get(position).getPlayTypeID().startsWith("45")) {
			holder.ivTypeId.setBackgroundResource(R.drawable.logo_sfc);
			holder.tv_item_documentary.setText("北单");
			holder.tv_item_documentary.setEnabled(false);
		}else{
			holder.tv_item_documentary.setText("跟单");
			holder.tv_item_documentary.setEnabled(true);
			holder.ivTypeId.setBackgroundResource(R.drawable.bet_loty_logo_jclq_main);
		}
		final String playTypeId = topicBeans.get(position).getPlayTypeID();
		Pattern pattern = Pattern.compile("1[0-9]{10}");
		final String name = topicBeans.get(position).name;
		// String name = topicBeans.get(position).name;
		final String isname;
		if (isMobileNO(name)) {
			isname = name.substring(0, 3) + "****" + name.substring(7, name.length());
			// holder.tv_name.setText());
		} else {
			isname = name;
			// holder.tv_name.setText(name.substring(0, 2)+"**");
		}
		// if (name.contains("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$")) {
		
		
		
		Spanned spanned1;
		if (topicBeans.get(position).hb.equals("1")) {
			holder.ivHongbaoShow.setVisibility(View.VISIBLE);
			
		spanned1 = Html
					.fromHtml(AppTools.changeStringColor("#e3393c",isname) );
		}else {
			holder.ivHongbaoShow.setVisibility(View.GONE);
			 spanned1 = Html
					.fromHtml(AppTools.changeStringColor("#105cde", isname));
		}
		
		holder.tv_name.setText(spanned1);
		if (topicBeans.get(position).detail.equals("")) {
			holder.tv_detail.setVisibility(View.GONE);
			holder.tv_detail.setText(AppTools.removeTheBlankOfString(topicBeans.get(position).detail));
		} else {
			holder.tv_detail.setVisibility(View.VISIBLE);
			holder.tv_detail.setText(topicBeans.get(position).detail);
		}

		String stopGuanzhu = topicBeans.get(position).interest;
		if (stopGuanzhu.equals("1")) {
			// holder.tvGuanzhu.setEnabled(true);
			holder.tvGuanzhu.setText(" +关注 ");
			holder.tvGuanzhu.setTextColor(context.getResources().getColor(R.color.red));
		} else {
			// holder.tvGuanzhu.setEnabled(false);
			holder.tvGuanzhu.setText("已关注");
			holder.tvGuanzhu.setTextColor(context.getResources().getColor(R.color.gaunzhucolor));
		}
		holder.tvGuanzhu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Toast.makeText(context, "5556", Toast.LENGTH_SHORT).show();
				info = topicBeans.get(position).userid + "";
				selectLoctonParse = position;
				guanzhuAsynTask = new GuanzhuAsynTask();
				guanzhuAsynTask.execute();
			}
		});

		String stoptime = topicBeans.get(position).stoptime;

		holder.tv_stoptime.setText(stoptime.substring(5, 16) + " 截止");
		String datatime = topicBeans.get(position).datetime;
		String daString = datatime.substring(5, 7) + "月" + datatime.substring(8, 10) + "日 " + datatime.substring(11, 13)
				+ "点" + datatime.substring(14, 16) + "分";
		holder.tv_circle_time.setText(daString);
		holder.tv_Total_amount.setText(schemeinfoBeans.get(position).buymoney + "元");// 总钱数

		String up = topicBeans.get(position).up;

		SpannableStringBuilder builder = new SpannableStringBuilder();
		List<CommentBean> commentBeans = new ArrayList<>();
		if(commentBeanLists != null && commentBeanLists.size() > 0){
			commentBeans = commentBeanLists.get(position);
		}
		if (commentBeans.isEmpty() && up.equals("")) {
			holder.digCommentBody.setVisibility(View.GONE);
			holder.snsBtn.setBackgroundResource(R.drawable.prise2);
			holder.snsBtn.setEnabled(true);
		} else {
			holder.digCommentBody.setVisibility(View.VISIBLE);
		}
		if (!up.equals("") && up != null) {
			holder.tvpraiseTextView.setVisibility(View.VISIBLE);

			if (up.contains(App.user.getName())) {
				holder.snsBtn.setBackgroundResource(R.drawable.prise);
				holder.snsBtn.setEnabled(false);
			} else {
				holder.snsBtn.setBackgroundResource(R.drawable.prise2);
				holder.snsBtn.setEnabled(true);
			}

			// 构造多个超链接的html, 通过选中的位置来获取用户名
			StringBuilder sbBuilder = new StringBuilder();
			// for (int i = 0; i < 10; i++) {
			// sbBuilder.append("username-" + i + "、");
			// }

			// builder.append(setImageSpan());
			String ups = up.substring(0, up.length() - 1);
			String[] up2 = ups.split(",");
			String names;
			for (int i = 0; i < up2.length; i++) {
				if (isMobileNO(up2[i])) {
					names = up2[i].substring(0, 3) + "****" + up2[i].substring(7, up2[i].length());
					// holder.tv_name.setText());
					if (i == up2.length - 1) {
						sbBuilder.append(names + ",");
						// builder.append(names);
					} else {
						sbBuilder.append(names + ",");
						// builder.append(names + ",");
					}

				} else {
					names = up2[i];
					// holder.tv_name.setText(name.substring(0, 2)+"**");
					if (i == up2.length - 1) {
						// builder.append(names);
						sbBuilder.append(names + ",");
					} else {
						sbBuilder.append(names + ",");
						// builder.append(names + ",");
					}

				}
			}
			String likeUsers = sbBuilder.substring(0, sbBuilder.lastIndexOf(",")).toString();
			holder.tvpraiseTextView.setMovementMethod(LinkMovementMethod.getInstance());
			holder.tvpraiseTextView.setText(addClickablePart(likeUsers), BufferType.SPANNABLE);
			// holder.tvpraiseTextView.setText(builder);
		} else {
			holder.tvpraiseTextView.setVisibility(View.GONE);

			holder.snsBtn.setBackgroundResource(R.drawable.prise2);
			holder.snsBtn.setEnabled(true);
		}

		if (!commentBeans.isEmpty()) {
			holder.commentList.setVisibility(View.VISIBLE);
			holder.lin_dig.setVisibility(View.VISIBLE);
			MylinAdapter mylinAdapter = new MylinAdapter(commentBeans, position);
			holder.commentList.setAdapter(mylinAdapter);
			// mylinAdapter.notifyDataSetInvalidated();
		} else {
			holder.commentList.setVisibility(View.GONE);
			holder.lin_dig.setVisibility(View.GONE);
		}

		String passTypeInfo = schemeinfoBeans.get(position).passTypeInfo;
		String preBetType = schemeinfoBeans.get(position).preBetType;
		String pasString = passTypeInfo.split(",")[passTypeInfo.split(",").length - 1];
		int multiple = Integer.parseInt(pasString.substring(0, pasString.length() - 1));
		int buymoney = Integer.parseInt(schemeinfoBeans.get(position).buymoney);
		final int Price = buymoney / multiple;


		//预计奖金改成跟单热度

//		holder.tv_Projected_bonus.setText(topicBeans.get(position).yjjj + "元");
		int followNum = 0;
		if(topicBeans.get(position).follCount != null){
			followNum = Integer.parseInt(topicBeans.get(position).follCount);
		}
		if (followNum<10){
			holder.tv_Projected_bonus.setText( "一星");
		}else if(followNum<20){
			holder.tv_Projected_bonus.setText( "二星");
		}else if(followNum<30){
			holder.tv_Projected_bonus.setText( "三星");
		}else if(followNum<40){
			holder.tv_Projected_bonus.setText( "四星");
		}else {
			holder.tv_Projected_bonus.setText( "五星");

		}




		holder.tv_price.setText(Price + "元");
		final int cid = topicBeans.get(position).id;

		String wins = topicBeans.get(position).Wins;

		if (Integer.parseInt(wins) > 1) {
			holder.ll_even_red.setVisibility(View.VISIBLE);
			holder.tv_number.setText(wins);
		} else {
			holder.ll_even_red.setVisibility(View.GONE);
		}


//		if()
		Log.e("caizhong", "getView: "+topicBeans.get(position).schemeid);

		holder.tv_item_documentary.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// MyToast.getToast(context, topicBeans.get(position).id+"跟单").show();
				Intent intent = new Intent(context, DocumentaryActivity.class);
				intent.putExtra("cid", cid);
				intent.putExtra("name", isname);
				intent.putExtra("Price", Price);
				intent.putExtra("playtypeid", playTypeId);
				context.startActivity(intent);
			}
		});

		// final SnsPopupWindow snsPopupWindow = new SnsPopupWindow(context);
		// 判断是否已点赞
		// final boolean curUser = up.contains(App.user.getName());

		// snsPopupWindow.update();
		// snsPopupWindow.setmItemClickListener(new
		// PopupItemClickListener(circlePosition, circleItem, curUserFavortId));

		holder.snsBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				// new SnsPopupWindow(context).showPopupWindow(v,topicBeans.get(position).id);

				if (App.user == null) {
					Intent intent;
					MyToast.getToast(context, "请先登陆").show();
					intent = new Intent(context, LoginActivity.class);
					intent.putExtra("loginType", "bet");
					context.startActivity(intent);
				} else {
					info = topicBeans.get(position).id + "";
					selectLoctonParse = position;
					mMyAsynTask = new MyAsynTask();
					mMyAsynTask.execute();

				}

			}
		});
		holder.snsBtnSay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				// new SnsPopupWindow(context).showPopupWindow(v,topicBeans.get(position).id);

				if (App.user == null) {
					Intent intent;
					MyToast.getToast(context, "请先登陆").show();
					intent = new Intent(context, LoginActivity.class);
					intent.putExtra("loginType", "bet");
					context.startActivity(intent);
				} else {
					selectLoctonParse = position;
					Intent intent = new Intent(context, CommentActivity.class);
					intent.putExtra("id", topicBeans.get(position).id);
					intent.putExtra("position", position);
					intent.putExtra("size", 1);
					context.startActivity(intent);
					// backListen.onClick(topicBeans.get(position).id, position);
				}

			}
		});

		ImageManager.getInstance().displayImage(App.url + topicBeans.get(position).HeadUrl, holder.iv_Head,
				ImageManager.getViewsHeadOptions());
		holder.iv_Head.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putString("userId", topicBeans.get(position).userid + "");

				Intent intent = new Intent(context, CirclePersonInfo.class);
				intent.putExtras(bundle);
				// intent.putExtra("userId", topicBeans.get(position).id+"");
				context.startActivity(intent);

			}
		});
		return view;
	}

	private SpannableString setImageSpan() {
		String text = "  ";
		SpannableString imgSpanText = new SpannableString(text);
		imgSpanText.setSpan(new ImageSpan(context, R.drawable.social_fave_blue, DynamicDrawableSpan.ALIGN_BASELINE), 0,
				1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		return imgSpanText;
	}

	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("1[0-9]{10}");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	/** 静态类 */
	static class ViewHolder {
		XCRoundImageView iv_Head;
		TextView tv_name;
		TextView tv_detail;
		TextView tv_Total_amount;
		TextView tv_Projected_bonus;// 最高奖金
		TextView tv_price;// 截止日期
		TextView tv_item_documentary;// 跟单
		TextView tvpraiseTextView;
		View lin_dig;
		TextView tv_number;
		MyListView2 commentList;
		TextView tv_stoptime;
		TextView tv_circle_time;// 时间
		ImageView snsBtn;// 点赞和评论
		ImageView snsBtnSay;// 点赞和评论
		SnsPopupWindow snsPopupWindow;
		LinearLayout digCommentBody;
		LinearLayout ll_even_red;
		ImageView ivTypeId;
		TextView tvGuanzhu;
		
		ImageView ivHongbaoShow;
	}

	// 关注
	class GuanzhuAsynTask extends AsyncTask<Integer, Integer, String> {

		String error = "-11";

		@Override
		protected String doInBackground(Integer... params) {

			opt = "90";
			time = RspBodyBaseBean.getTime();
			imei = RspBodyBaseBean.getIMEI(context);

			// info = RspBodyBaseBean.change_TheSun(schemeId+"", context_Sun);

			// String key = MD5.md5(App.user.getUserPass() + App.MD5_key);
			String key = App.key;
			crc = RspBodyBaseBean.getCrc(time, imei, key, info, App.user.getUid());
			auth = RspBodyBaseBean.getAuth(crc, time, imei, App.user.getUid());
			String[] values = { opt, auth, info };
			Log.i(TAG, "晒单" + RequestParams.convertParams(context, opt, info));
			String result = HttpUtils.doPost(App.names, values, App.path);

			System.out.println("晒单：" + result);
			ganzhu = result;
			//
			// if (result.equals("success")) {
			// error = "10";
			// }

			return error;
		}

		@Override
		protected void onPostExecute(String result) {

			gzmHandler.sendEmptyMessage(Integer.parseInt(result));

			super.onPostExecute(result);
		}
	}

	// 点赞
	public String ganzhu;

	class MyAsynTask extends AsyncTask<Integer, Integer, String> {

		String error = "-1";

		@Override
		protected String doInBackground(Integer... params) {

			opt = "87";
			time = RspBodyBaseBean.getTime();
			imei = RspBodyBaseBean.getIMEI(context);

			// info = RspBodyBaseBean.change_TheSun(schemeId+"", context_Sun);

			// String key = MD5.md5(App.user.getUserPass() + App.MD5_key);
			String key = App.key;
			crc = RspBodyBaseBean.getCrc(time, imei, key, info, App.user.getUid());
			auth = RspBodyBaseBean.getAuth(crc, time, imei, App.user.getUid());
			String[] values = { opt, auth, info };
			Log.i(TAG, "晒单" + RequestParams.convertParams(context, opt, info));
			String result = HttpUtils.doPost(App.names, values, App.path);
			Log.e(TAG, "晒单：" + result);
			if (result.equals("success")) {
				error = "0";
			}

			return error;
		}

		@Override
		protected void onPostExecute(String result) {

			mHandler.sendEmptyMessage(Integer.parseInt(result));

			super.onPostExecute(result);
		}
	}

	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 0:
				if (topicBeans.get(selectLoctonParse).getUp() == "") {
					topicBeans.get(selectLoctonParse)
							.setUp(topicBeans.get(selectLoctonParse).getUp() + App.user.getName());
				} else {
					topicBeans.get(selectLoctonParse)
							.setUp(topicBeans.get(selectLoctonParse).getUp() + App.user.getName() + " ");
				}
				notifyDataSetChanged();
				MyToast.getToast(context, "点赞成功").show();
				break;
			case 1:
				// MyToast.getToast(context, "重复晒单").show();
				break;
			case -2:

				break;
			case -1:

				break;
			case -11:

				break;
			case 10:

				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	}

	@SuppressLint("HandlerLeak")
	class guanHuMyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 0:

				break;
			case 1:
				// MyToast.getToast(context, "重复晒单").show();
				break;
			case -2:

				break;
			case -1:

				break;
			case -11:
				Toast.makeText(context, ganzhu, Toast.LENGTH_SHORT).show();
				if (ganzhu.equals("关注成功")) {
					topicBeans.get(selectLoctonParse).setInterest("0");
				} else if (ganzhu.equals("取消关注")) {
					topicBeans.get(selectLoctonParse).setInterest("1");
				}
				notifyDataSetChanged();
				break;
			case 10:

				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	}

	int onClickPosition;

	class MylinAdapter extends BaseAdapter {

		List<CommentBean> commentBeans;
		int setPosition;

		public MylinAdapter(List<CommentBean> commentBeans, int setPosition) {
			super();
			this.commentBeans = commentBeans;
			this.setPosition = setPosition;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return commentBeans.size();
		}

		@Override
		public Object getItem(int position) {

			return position;
		}

		@Override
		public long getItemId(int position) {

			return position;
		}

		@Override
		public View getView(final int position, View view, ViewGroup parent) {
			ViewHolder4 holder4 = null;
			if (view == null) {
				holder4 = new ViewHolder4();
				view = LayoutInflater.from(context).inflate(R.layout.item_comment, null);
				holder4.tv_comment = (TextView) view.findViewById(R.id.commentTv);

				view.setTag(holder4);

			} else {
				holder4 = (ViewHolder4) view.getTag();
			}

			holder4.tv_comment.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					onClickPosition = position;

					if (App.user == null) {
						Intent intent;
						MyToast.getToast(context, "请先登陆").show();
						intent = new Intent(context, LoginActivity.class);
						intent.putExtra("loginType", "bet");
						context.startActivity(intent);
					} else {
						selectLoctonParse = position;

						if (App.user.getUid().equals("" + commentBeans.get(position).uid)) {

							Intent intent = new Intent(context, CommentActivity.class);
							intent.putExtra("id", topicBeans.get(setPosition).id);
							intent.putExtra("position", setPosition);
							intent.putExtra("size", 1);
							context.startActivity(intent);
						} else {

							Intent intent = new Intent(context, RECommentActivity.class);
							intent.putExtra("id", topicBeans.get(setPosition).id);
							intent.putExtra("pid", commentBeans.get(position).getId());
							intent.putExtra("name", commentBeans.get(position).name);
							intent.putExtra("position", setPosition);
							intent.putExtra("size", 1);
							context.startActivity(intent);
							// backListen.onClick(topicBeans.get(position).id, position);\
						}
					}
				}
			});
			

			String name = commentBeans.get(position).name;
			String isname;
			if (isMobileNO(name)) {
				isname = name.substring(0, 3) + "****" + name.substring(7, name.length());
				// holder.tv_name.setText());
			} else {
				isname = name;
				// holder.tv_name.setText(name.substring(0, 2)+"**");
			}

			String REname = commentBeans.get(position).getRname();
			
			String REisname = "";
			if (!REname.equals("null") && !REname.equals("") && REname != null) {
				if (isMobileNO(REname)) {
					REisname = REname.substring(0, 3) + "****" + REname.substring(7, REname.length());
					// holder.tv_name.setText());
				} else {
					REisname = REname;
					// holder.tv_name.setText(name.substring(0, 2)+"**");
				}
			}

			String detail = commentBeans.get(position).detail;
			StringBuilder sbBuilder = new StringBuilder();

			sbBuilder.append(isname + "回复");
			if (!REisname.equals("") && REisname != null) {
				sbBuilder.append(REisname + "回复");
			}

			String[] ids = { commentBeans.get(position).uid + "", commentBeans.get(position).ruid };
			String likeUsers = sbBuilder.substring(0, sbBuilder.lastIndexOf("回复")).toString();
			holder4.tv_comment.setMovementMethod(LinkMovementMethod.getInstance());
			holder4.tv_comment.setText(addClickablePartbyAda(likeUsers, detail, ids), BufferType.SPANNABLE);

			// holder4.tv_comment.setText(Html.fromHtml("<font color=\"#8290AF\">" + isname
			// + ":</font>" + detail));
			return view;
		}

		class ViewHolder4 {
			TextView tv_comment;
		}

		/**
		 * @param str
		 * @return
		 */
		private SpannableStringBuilder addClickablePartbyAda(String str, String detail, String[] ids) {

			SpannableStringBuilder ssb = new SpannableStringBuilder();
			ssb.append(str);

			String[] likeUsers = str.split("回复");

			if (likeUsers.length > 0) {
				// 最后一个
				for (int i = 0; i < likeUsers.length; i++) {
					final String name = likeUsers[i];
					final String nameid = ids[i];
					final int start = str.indexOf(name);
					ssb.setSpan(new ClickableSpan() {

						@Override
						public void onClick(View widget) {

							Bundle bundle = new Bundle();
							bundle.putString("userId", nameid);

							Intent intent = new Intent(context, CirclePersonInfo.class);
							intent.putExtras(bundle);
							// intent.putExtra("userId", topicBeans.get(position).id+"");
							context.startActivity(intent);

						}

						@Override
						public void updateDrawState(TextPaint ds) {
							super.updateDrawState(ds);
							ds.setColor(context.getResources().getColor(R.color.praise_item)); // 设置文本颜色
							// 去掉下划线
							ds.setUnderlineText(false);
						}

					}, start, start + name.length(), 0);
				}
			}
			return ssb.append(":" + detail);
		} // end of addClick

	}

}
